package v2;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import pipeline.JsonFilePipeline;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.scheduler.PriorityScheduler;
import util.BaseUtil;
import vo.PinPaiVO;

public class SnygMain {
	
	private static final Logger log = Logger.getLogger(SnygMain.class);
	
	public static void main(String[] args) {
		long startTime = new Date().getTime();
		if (args.length < 3){
			System.out.println("<inputFile><encode><outputFilePath>");
			return;
		}
		
		String inputFilePath = args[0];
		String encode = args[1];
		List<String> needPinPais = BaseUtil.readFileText(inputFilePath, encode);
		
		//1、获取所有手机品牌的入口url
		SnygPinPaiUrlListPageProcessor snygPinPaiUrlListPageProcessor = new SnygPinPaiUrlListPageProcessor();
		Map<String, PinPaiVO> pinPaiMap = snygPinPaiUrlListPageProcessor.getPinPaiMap();
		
		if(pinPaiMap != null){
			String outputFilePath = args[2];
			for(Map.Entry<String, PinPaiVO> entry : pinPaiMap.entrySet()){
				String name = entry.getValue().getName();
				String pinpaiUrl = entry.getValue().getUrl(); 
				//2、判断是否是要抓取的品牌，是则继续
				String pinPai = BaseUtil.toCrawl(name, needPinPais);
				if("".equals(pinPai)){
					continue;
				}
				System.out.println(pinPai);
				//3、输出手机信息到相应文件中
				String fileName = pinPai+".json";
				
				SnygPageProcessor snygPageProcessor = new SnygPageProcessor();
				snygPageProcessor.setEntryUrl(pinpaiUrl);
				Spider spider = Spider.create(snygPageProcessor).scheduler(new PriorityScheduler())
					.addPipeline(new JsonFilePipeline(outputFilePath, fileName));
				spider.addUrl(pinpaiUrl);
				spider.thread(2).run();
				
				
			}
		}
		long endTime = new Date().getTime();
		log.info("苏宁易购执行时间："+String.valueOf((endTime-startTime)));
	}
}
